<template>
  <div>
    <common-head title="商品详情" class="title" />
    <div class="con">
      <div class="list-head">
        <div :class="['price',{active:active.orderBy === 0}]" @click="byPrice">
          <span>价格</span>
          <div :class="['angle','up',{
            active:active.orderBy === 0 && active.upDown === 1
          }]"></div>
          <div :class="['angle','down',{
            active:active.orderBy === 0 && active.upDown === 0
          }]"></div>
        </div>
        <div :class="['sale',{active:active.orderBy === 1}]" @click="bySale">
          <span>销量</span>
          <div :class="['angle','up',{
            active:active.orderBy === 1 && active.upDown === 1
          }]"></div>
          <div :class="['angle','down',{
            active:active.orderBy === 1 && active.upDown === 0
          }]"></div>
        </div>
      </div>

      <!-- 商品主体 -->
      <h1><img src="@assets/image/157793128909842388.png" alt=""></h1>
      <div class="bottom">
        <div class="con" v-for="item in items" :key="item.id" @click="enterDetail(item.id)">
          <img :src="item.pic" alt="">
          <div class="c-bttom">
            <p>
              {{item.name}}
            </p>
            <div class="evaluate">
              {{item.tags}}
            </div>
            <div class="price">
              <span class="money">{{item.minPrice | currency}}</span>
              <span class="volume">10+评价</span>
            </div>
          </div>
        </div>
      </div>

    </div>
    {{$route.params.categoryId}}

    <my-tabbar></my-tabbar>
  </div>
</template>

<script>
import MyTabbar from '@components/MyTabbar'
import CommonHead from '@components/CommonHead'
import { getItemLists } from '@api'
import {currency} from '@filters'

export default {
  data () {
    return {
      items:[],
      active: {
        orderBy: 0,// 0 价格  1 销量
        upDown: 0,// 0 降序  1 升序
      }
    }
  },
  filters: {
    currency
  },
  created () {
    this.fetchItems()
  },
  methods: {
    fetchItems () {
      getItemLists({
        categoryId: this.$route.params.categoryId,
        orderBy: this.orderBy
      }).then(res => {
        console.log(res);
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    // 点击改变上下箭头
    byPrice () {
      // 点击 价格排序
      this.active.orderBy = 0
      if (this.active.upDown === 0) {
        this.active.upDown = 1
      } else {
        this.active.upDown = 0
      }
      this.fetchItems()
    },
    bySale () {
      // 点击 销量排序
      this.active.orderBy = 1
      if (this.active.upDown === 0) {
        this.active.upDown = 1
      } else {
        this.active.upDown = 0
      }
      this.fetchItems()
    },
    // 点击进入详情页
    enterDetail(id){
      this.$router.push(`/detail/${id}`)
    }
  },
  computed: {
    orderBy () {
      let orderBy = 'priceUp'
      if (this.active.orderBy === 0 && this.active.upDown === 0) {
        orderBy = 'priceUp'
      } else if (this.active.orderBy === 0 && this.active.upDown === 1) {
        orderBy = 'priceDown'
      } else if (this.active.orderBy === 1 && this.active.upDown === 0) {
        orderBy = 'ordersUp'
      } else {
        orderBy = 'ordersDown'
      }
      return orderBy
    }
  },
  components: {
    MyTabbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.list-head {
  line-height: 40px;
  background: rgb(240, 236, 236);
  display: flex;
  font-size: 14px;
  justify-content: space-around;
  .angle {
    width: 0;
    height: 0;
    border: 6px solid transparent;
    &.up {
      border-bottom-color: #999;
    }
    &.down {
      border-top-color: #999;
    }
  }
  .price,
  .sale {
    position: relative;
    &.active {
      color: #f66;
    }
  }
  .angle {
    position: absolute;
    right: -15px;
  }
  .angle.up {
    top: 8px;
    &.active {
      border-bottom-color: #f66;
    }
  }
  .angle.down {
    top: 22px;
    &.active {
      border-top-color: #f66;
    }
  }
}
h1 {
  margin: 20px 0 10px 0;
  img {
    width: 100%;
  }
}

.bottom {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 0 10px;
  justify-content: space-between;
  .con {
    width: 49%;
    box-sizing: border-box;

    margin-bottom: 5px;
    background-color: #fff;
    border-radius: 5px;
   
    img {
      width: 100%;
      height: 150px;
      border-radius: 5px 5px 0 0;
    }
    .c-bttom {
      box-sizing: border-box;
      padding: 0 5px;
      p {
        font-size: 14px;
        color: #333;
        font-weight: 550;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        line-height: 20px;
      }
      .evaluate {
          width: 57px;
          height: 10px;
        font-size: 10px;
        text-align: center;
        line-height: 10px;
        border-radius: 2px;
        border: 1px solid #ffbb00;
        margin-top: 5px;
      }
      .price {
        margin: 10px 0;
        .money {
          font-size: 16px;
          color: red;
          font-weight: 550;
        }
        .volume {
          margin-left: 5px;
          color: #777;
        }
      }
    }
  }
}
</style>
